//
//  BDMNetworkManager.m
//  iJobs
//
//  Created by Li Xiaopeng on 15/1/22.
//  Copyright (c) 2015年 liyingpeng. All rights reserved.
//

#import "BDMNetworkManager.h"
#import "BDMHTTPRequestOperationManager.h"

@interface BDMNetworkManager()

@property(nonatomic, strong) BDMHTTPRequestOperationManager *operationManager;

@property (nonatomic, strong) NSMutableArray *requestIdList;

@end

@implementation BDMNetworkManager

+ (BDMNetworkManager *)sharedInstance
{
    static id _sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedInstance = [[self alloc] init];
    });
    return _sharedInstance;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        _operationManager = [BDMHTTPRequestOperationManager manager];
        [_operationManager urlCacheInitialization];
    }
    return self;
}

- (NSMutableArray *)requestIdList
{
    if (_requestIdList == nil) {
        _requestIdList = [[NSMutableArray alloc] init];
    }
    return _requestIdList;
}


- (void)cancelRequestForIdentifier:(NSString *)aIdentifier
{
    [_operationManager cancelRequestForIdentifier:aIdentifier];
    [self.requestIdList removeObject:aIdentifier];
}

- (void)cancelRequestForIdentifierList:(NSMutableArray *)requestIDList {
    [_operationManager cancelRequestForIdentifierList:self.requestIdList];
    [self.requestIdList removeAllObjects];
}

- (void)cancelAllRequest
{
    [_operationManager cancelAllRequest];
}

- (void)urlCacheInitialization
{
    [_operationManager urlCacheInitialization];
}

- (void)clearAllURLCache
{
    [_operationManager clearAllURLCache];
}

-(void)sendHttpRequestWithModel:(BDMHTTPRequestModel *) aRequestModel
                        success:(void (^)(id responseObject))aSuccessBlock
                        failure:(void (^)(BDMError *error))aFailureBlock
{
    if (![BDMUtility isEmptyString:aRequestModel.identifier]) {
        [self.requestIdList addObject:aRequestModel.identifier];
    }
    [_operationManager sendHttpRequestWithModel:aRequestModel success:aSuccessBlock failure:aFailureBlock];
}

- (void)sendHttpRequestGroupWithModelArray:(NSArray<BDMHTTPRequestModel *> *)modelArray
                                  progress:(void(^)(id responseObject, NSString *identifier, BDMError *error))progress
                                  complete:(void(^)(BOOL isRequestSuccess))complete
                          isResultRestrict:(BOOL)isResultRestrict {
    for (BDMHTTPRequestModel *model in modelArray) {
        if (![BDMUtility isEmptyString:model.identifier]) {
            [self.requestIdList addObject:model.identifier];
        }
    }
    [_operationManager sendHttpRequestGroupWithModelArray:modelArray progress:progress complete:complete isResultRestrict:isResultRestrict];
}

- (void)uploadFileWithFileModel:(BDMUploadFileModel *)aUploadFileModel
              progressCallBack:(void (^)(NSUInteger __unused bytesWritten,
                                         long long totalBytesWritten,
                                         long long totalBytesExpectedToWrite))progressBlock
                       success:(void (^)(id responseObject))aSuccessBlock
                       failure:(void (^)(NSError *error))aFailureBlock
{
    [_operationManager uploadFileWithFileModel:aUploadFileModel progressCallBack:progressBlock success:aSuccessBlock failure:aFailureBlock];
}

- (void)downloadFileWithFileModel:(BDMDownloadFileModel *)aDownloadModel
                 progressCallBack:(void (^)(NSUInteger bytesRead,
                                            long long totalBytesRead,
                                            long long totalBytesExpectedToRead))progressBlock
                          success:(void (^)(id responseObject))aSuccessBlock
                          failure:(void (^)(NSError *error))aFailureBlock
{
    [_operationManager downloadFileWithFileModel:aDownloadModel progressCallBack:progressBlock success:aSuccessBlock failure:aFailureBlock];
}

- (void)startSendHeartbeat {
    if ([_operationManager respondsToSelector:@selector(startSendHeartbeat)]) {
        [_operationManager performSelector:@selector(startSendHeartbeat) withObject:nil];
    }
}

- (void)stopSendHeartbeat {
    if ([_operationManager respondsToSelector:@selector(stopSendHeartbeat)]) {
        [_operationManager performSelector:@selector(stopSendHeartbeat) withObject:nil];
    }
}

@end
